/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @author 品讯科技
 */
package com.px.pa.modulars.circle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.pa.modulars.circle.entity.CircleActivity;
import com.px.pa.modulars.circle.entity.CircleActivitySign;
import com.px.pa.modulars.circle.mapper.CircleActivitySignMapper;
import com.px.pa.modulars.circle.service.CircleActivityService;
import com.px.pa.modulars.circle.service.CircleActivitySignService;
import com.px.pa.modulars.circle.service.CircleNoticeConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Service
public class CircleActivitySignServiceImpl extends ServiceImpl<CircleActivitySignMapper, CircleActivitySign> implements CircleActivitySignService {

    @Resource
    private CircleActivityService circleActivityService;
    @Resource
    private CircleActivitySignService circleActivitySignService;
    @Resource
    private CircleNoticeConfigService circleNoticeConfigService;
    @Override
    public void get(Integer id) {
      CircleActivity circleActivity = circleActivityService.getById(id);
        int count =  circleActivitySignService.count(new QueryWrapper<CircleActivitySign>().eq("activity_id",id));
        if(circleActivity.getLeastPeople() > count){
            circleActivity.setIsAbandoned(1);//1.废弃
            circleActivityService.updateById(circleActivity);
        }
    }

    /**
     * 时间差
     * @return
     */
    @Override
    public int getTime1(Date endTime)  {
        Date date = new Date();
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");//如2016-08-10 20:40
        String fromDate = simpleFormat.format(date);
        String toDate = simpleFormat.format(endTime);
        long from = 0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long to = 0;
        try {
            to = simpleFormat.parse(toDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int minutes = (int) ((to - from)/(1000 * 60));
        return minutes;
    }


    /**
     * 时间差
     * @return
     */
    @Override
    public int getTime(Date startTime,Date endTime)  {
        //Date date = new Date();
       // Date startDate = Date.from(startTime.toInstant(ZoneOffset.of("+8")));
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");//如2016-08-10 20:40
        String fromDate = simpleFormat.format(startTime);
        String toDate = simpleFormat.format(endTime);
        long from = 0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long to = 0;
        try {
            to = simpleFormat.parse(toDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int minutes = (int) ((to - from)/(1000 * 60));
        return minutes;
    }

    @Override
    public void companyAcSignRelease(Integer aid) {
      CircleActivity circleActivity  = circleActivityService.getById(aid);
        circleNoticeConfigService.companyAcSignRelease(circleActivity.getTotle(),circleActivity.getDetails(),circleActivity.getId(),circleActivity.getTenantCode());
    }

    @Override
    public void companyAcStart(Integer aid) {
        CircleActivity circleActivity  = circleActivityService.getById(aid);
        circleNoticeConfigService.companyAcStart(circleActivity.getTotle(),circleActivity.getDetails(),circleActivity.getId(),circleActivity.getTenantCode());
    }

    @Override
    public List<CircleActivitySign> exportUsers(Integer activityId, Integer cancel) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("a.activity_id", activityId);
        queryWrapper.ne("a.is_cancel", cancel);
        queryWrapper.orderByDesc("a.create_time");
        return super.baseMapper.exportUsers(queryWrapper);
    }


}
